﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using EFERP;
using HRM.DICTIONARY.DAL;

namespace HRM.DICTIONARY.UI
{
    public partial class xfrmSYSUSER : DevExpress.XtraEditors.XtraForm
    {
        ICommon<sys_user> exeDic;
        sys_user currObj = null;
        private bool isChooseRow = false;

        public xfrmSYSUSER()
        {
            InitializeComponent();
        }

        private void xfrmSYSUSER_Load(object sender, EventArgs e)
        {
            exeDic = new Common<sys_user>(StaticVariable.ERPEn);
            exeDic.GV = gbList;
            exeDic.GC = gcList;
            exeDic.ColIDName = "userid";
            Reload();
        }

        private void Add()
        {
            xfrmSYSUSERAdd frm = new xfrmSYSUSERAdd(exeDic);
            frm.ShowDialog();
            if (exeDic.myAction == Actions.Insert || exeDic.myAction == Actions.InsertMultiple)
            {
                exeDic.myAction = Actions.View;
            }
            Reload();
        }
        private void Edit()
        {
            xfrmSYSUSERAdd frm = new xfrmSYSUSERAdd(currObj, exeDic);
            frm.ShowDialog();
            if (exeDic.myAction == Actions.Update)
            {
                exeDic.myAction = Actions.View;
                Reload();
            }
            isChooseRow = false;
            DoShowOrHide(isChooseRow);
        }
        private void Delete()
        {
            bool KQ = exeDic.DeleteOnGridView(currObj);
            if (KQ)
            {
                isChooseRow = false;
                DoShowOrHide(isChooseRow);
            }
            else
            {
                MessageBox.Show("Không thể xóa dữ liệu");
            }
        }
        private void Reload()
        {
            if (exeDic != null)
            {
                exeDic.AddGridControlFromList();
                isChooseRow = false;
                currObj = null;
                DoShowOrHide(isChooseRow);
            }
        }
        public void Print()
        {
            if (gbList != null)
            {
                if (gbList.RowCount > 0)
                {
                    gbList.ShowPrintPreview();
                    isChooseRow = false;
                    currObj = null;
                    DoShowOrHide(isChooseRow);
                }
            }
        }
        public void Export()
        {
            if (exeDic != null)
            {
                exeDic.AddGridExport(this);
                isChooseRow = false;
                currObj = null;
                DoShowOrHide(isChooseRow);
            }
        }
        private void DoShowOrHide(bool isChoose)
        {
            bbiEdit.Enabled = isChoose;
            bbiDelete.Enabled = isChoose;
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Dispose();
                    return true;

                case Keys.F1:
                    //Help Link Click
                    return true;

                case Keys.F5:
                    this.Reload();
                    return true;

                case (Keys.Control | Keys.E):
                    this.Export();
                    return true;

                case (Keys.Control | Keys.F):
                    //Search
                    return true;

                case (Keys.Control | Keys.N):
                    //Call Form Add
                    return true;

                case (Keys.Control | Keys.P):
                    this.Print();
                    return true;

                case (Keys.Alt | Keys.D):
                    MessageBox.Show("You are master ");
                    return true;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    return true;
            }
            return false;
        }

        private void bbiAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
        }
        private void bbiEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }
        private void bbiDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }
        private void bbiRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Reload();
        }
        private void bbiPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Print();
        }
        private void bbiExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Export();
        }
        private void bbiClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Dispose();
        }

        private void gbList_Click(object sender, EventArgs e)
        {
            currObj = exeDic.gbList1_Click(sender, e);
            if (currObj == null)
            {
               // MessageBox.Show("Vui lòng chọn dòng trên bảng tính");
            }
            else
            {
                isChooseRow = true;
                DoShowOrHide(isChooseRow);
            }
        }
        private void gbList_DoubleClick(object sender, EventArgs e)
        {
            currObj = exeDic.gbList1_DoubleClick(sender, e);
            if (currObj == null)
            {
               // MessageBox.Show("Vui lòng chọn dòng trên bảng tính");
            }
            else
            {
                Edit();
            }
        }

        public void xfrmSYSUSER_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    Dispose();
                    break;

                case Keys.F1:
                //Help Link Click

                case Keys.F5:
                    this.Reload();
                    break;

                case (Keys.Control | Keys.E):
                    this.Export();
                    break;

                case (Keys.Control | Keys.F):
                    //Search
                    break;

                case (Keys.Control | Keys.N):
                    //Call Form Add
                    break;

                case (Keys.Control | Keys.P):
                    this.Print();
                    break;

                case (Keys.Alt | Keys.D):
                    MessageBox.Show("You are master ");
                    break;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    break;
            }
        }
    }
}